Alibabacloud.com offers a wide variety of articles about javascript getelementsbyclassname, easily find your javascript getelementsbyclassname information here online.
The problem is not supported for getelementsbyclassname in JavaScript in IE 6/7/8.Then the need to simulate the getelementsbyclassname needs to take the classname attribute, which involves the getattribute problem in JavaScript.In IE 6/7, there is a bug for getattributeNeed to use ClassName to get the following:var node = document.getElementById ("test"); var nam
xTable of Contents [1] using [2]classlist [3] to extend the preceding wordsSince there are getElementById () and getElementsByTagName () methods, why is there no getelementsbyclassname ()? The id attribute, tag name, and class attribute have no merit or disadvantage. Finally, HTML5 new Getelementsbyclassname () method, due to the widespread use of the class name in the CSS layout, the method just hit the pa
Getelementsbyclassname ()
To find our tree menu (perhaps multiple) from a large stack of HTML code, we first implement a way to find a DOM node through classname: Getelementsbyclassname. This is a simple but practical extension of the browser's own DOM approach.
This method has two parameters: Ele indicates which DOM node to look for (that is, only ele child nodes), classname indicates what classname must
Let's take a look at the code: (Supports multiple class queries and queries within a range)
Copy Code code as follows:
/*
* Get the element collection based on the element clsssname
* @param fatherid The ID of the parent element, default to document
* @tagName the label name of the child element
* @className className strings separated by spaces
*/
function Getelementsbyclassname (fatherid,tagname,classname) {
node = Fatheriddocume
Getelementsbyclassname ()
To read a large number of HTML
Code To find out the tree menu (maybe there are multiple), let's first implement a method to find the DOM node through classname: getelementsbyclassname. This is a simple but practical extension of the browser's own DOM method.
This method has two parameters: ele indicates which Dom node is used as the root node (that is, only the child node of EL
Let's take a look.
Code (Supports multiple class queries and queries within a certain range)
Copy code The Code is as follows :/*
* Obtain the Element Set Based on the element clsssname.
* @ Param fatherid the ID of the parent element. The default value is document.
* @ Tagname the label name of the sub-element
* @ Classname A classname string separated by Spaces
*/
Function getelementsbyclassname (fatherid, tagname, classname ){
Node = fatherid do
Let's take a look at the Code: (Multiple class queries and queries within a certain range are supported)Copy codeThe Code is as follows:/** Obtain the Element Set Based on the element clsssName.* @ Param fatherId the ID of the parent element. The default value is document.* @ TagName the label name of the sub-element* @ ClassName A className string separated by Spaces*/Function getElementsByClassName (fatherId, tagName, className ){Node = fatherId do
Try{varA = Document.getelementsbyclassname ("CLA"); Console.log (a);}Catch(ex) {var array = []; for(vari = 0;i){ if(Document.body.childnodes[i].nodetype = = 1){ varName =Document.body.childnodes[i].classname; if(Name.search (/cla/)! =-1) {Array.push (document.body.childnodes[i]); }}} console.log (array);} The HTML feature can be used to get the corresponding class element. Here we can see that document.body.childNodes is constantly being used, so simple optimization
A script to get elements based on classname on the web today is recorded here for later use.
Copy Code code as follows:
var getelementsbyclassname = function (searchclass, node, tag) {
if (docu Ment.getelementsbyclassname) {
return document.getelementsbyclassname (searchclass)
} else {
node = node || Document
Tag = Tag | | "*";
var classes = Searchclass.split (""),
elements = (Tag = = "*" node.all)? Node.all:n
The querySelector () method and querySelectorAll () method in HTML5 are described in the previous articles. I don't know how you understand them? The querySelector () method returns the first element that matches the passed CSS selector; querySelectorAll () the method returns all elements that match the passed CSS identifier and is a NodeList object. After a brief review, I will share with you the getElementsByClassName method, a new method.
As HTML4
Various implementation methods: Pure dom
Generally, getelementsbytagname ("*") is used to retrieve all elements in the document, traverse them, and use regular expressions to find matching elements and put them in an array to return the results. Because ie5 does not support document. getelementsbytagname ("*"), use the branch document. All to prevent errors.
The ultimate getelementsbyclassname SolutionThe author is Robert Nyman, which has been real
is sure to be very spirited in the future when the lower version of IE exits the market, it is best not to use this function directly at this moment, but we can simulate implementing this function.The code example is as follows:DOCTYPE HTML>HTML>Head>MetaCharSet= "Utf-8">Metaname= "Author"content= "http://www.51texiao.cn/" />title>Getelementsbyclassname () Usage details-ant tribetitle>Scripttype= "Text/javascript
Today, the use of Getelementsbyclassname wrote a small program, full of joy ready to test, in FF, Google and other mainstream browsers on the test is no problem, in the IE9 also no problem, in the IE6, 8 test when there is a problem, the browser error. Tangled up the code, seemingly no problem, determined to find the Niang ... Finally found the problem, it is IE6, 8 does not support getelementsbyclassname t
Internet Explorer does not support getElementsByClassName. getelementsbyclass
The getElementsByClassName method has been added to DOM3. However, other versions except IE9 and 10 do not support this method. This is a pain point!Currently, this solution can be used to determine that the browser does not support this method. If it does not support this method, add the getE
Currently, this solution can be used to determine that the browser does not support this method. If this method is supported, the getElementsByClassName method will be added to the document object, if you are interested, you can understand that this method can be used to determine that browser support does not support this method. If this method is not supported, add the getElementsByClassName Method to the
Q:getelementsbyclassname () in IE browser but normal use, display Web page script error.A: In the JS file to add the following processing code, it can be a perfect solution to this problem.//solve the problem that IE cannot use the Getelementsbyclassname () method//if the browser cannot use document. getelementsbyclassname () if(!document.getelementsbyclassname) {//then give the following value to the GC
First look at an example
The code is as follows
Copy Code
/** Get the element collection based on the element clsssname* @param fatherid The ID of the parent element, default to document* @tagName the label name of the child element* @className className strings separated by spaces*/function Getelementsbyclassname (fatherid,tagname,classname) {node = Fatheriddocument.getelementbyid (Fatherid) | | DocumentTagName = TagName
We know that native JS provides us with a getelementsbyclassname method that can be used to get a set of nodes with a specified class, note a collection, which returns an array of the functions.
However, IE does not support this method, but this method is very practical, so we have to specifically for IE to implement such a function.
Copy Code code as follows:
function Getelementsbyclassname
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.